package com.soft.service.impl;

import java.util.List;

import com.soft.dao.PermissionDao;
import com.soft.dao.StoreDao;
import com.soft.model.Permission;
import com.soft.service.IPermissionService;
import com.soft.util.Const;

public class PermissionServiceImpl implements IPermissionService {

	@Override
	public List<Permission> findAll() {
		String sql = "select id, permissionName, permissionContext, parentId, addtime, adduser from "
				+ Const.PERMISSION_TB;
		return PermissionDao.getPermission(sql);
	}

	@Override
	public Permission findById(int id) {
		String sql = "select id, permissionName, permissionContext, parentId, addtime, adduser from "
				+ Const.PERMISSION_TB + " where id=?";
		List<Permission> list = PermissionDao.getPermission(sql, id);
		Permission permission = new Permission();
		if (list != null && list.size() == 1) {
			permission = list.get(0);
		}
		return permission;
		
	}

	@Override
	public List<Permission> findByPid(int pid) {
		String sql = "select id, permissionName, permissionContext, parentId, addtime, adduser from "
				+ Const.PERMISSION_TB + " where parentId=?";
		return PermissionDao.getPermission(sql, pid);
	}

	@Override
	public boolean add(Permission permission) {
		String sql = "insert into " + Const.PERMISSION_TB
				+ "(permissionName, permissionContext, parentId, addtime, adduser) values (?,?,?,now(),?";
		String permissionName = permission.getPermissionName();
		String permissionContext = permission.getPermissionContext();
		int parentId = permission.getParentId();
		int userId = permission.getUserId();
		return PermissionDao.update(sql, permissionName, permissionContext, parentId, userId);
	}

	@Override
	public boolean delById(int id) {
		String sql = "delete from " + Const.PERMISSION_TB + " where id=?";
		return PermissionDao.update(sql, id);
	}

	@Override
	public boolean update(Permission permission) {
		int id = permission.getId();
		String permissionName = permission.getPermissionName();
		String permissionContext = permission.getPermissionContext();
		int parentId = permission.getParentId();
		int userId = permission.getUserId();
		String sql = "update " + Const.PERMISSION_TB
				+ " set permissionName=?,permissionContext=?,parentId=?,adduser=? where id=?";
		return PermissionDao.update(sql, permissionName, permissionContext, parentId, userId, id);
	}

	@Override
	public List<Permission> find(String permissionName, String parentId, int pageSize, String currPage) {
		String sql = "select id, permissionName, permissionContext, parentId, addtime, adduser from "
				+ Const.PERMISSION_TB + " where 1=1";
		StringBuffer sb = new StringBuffer(sql);
		if (permissionName != null && !"".equals(permissionName)) {
			sb.append(" and permissionName like'%" + permissionName + "%'");
		}
		if (parentId != null && !"".equals(parentId) && !"0".equals(parentId)) {
			sb.append(" and parentId =" + parentId);
		}
		int pageNum = 1;
		if (currPage != null && !"".equals(currPage)) {
			pageNum = Integer.parseInt(currPage);
		}
		sb.append(" limit ?,?");
		sql = sb.toString();
		int start = (pageNum - 1) * pageSize;
		return PermissionDao.getPermission(sql, start, pageSize);
	}

	@Override
	public int getCount(String permissionName, String parentId) {
		String sql = "select count(id) from " + Const.PERMISSION_TB + " where 1=1";
		StringBuffer sb = new StringBuffer(sql);
		if (permissionName != null && !"".equals(permissionName)) {
			sb.append(" and permissionName like'%" + permissionName + "%'");
		}
		if (parentId != null && !"".equals(parentId) && !"0".equals(parentId)) {
			sb.append(" and parentId =" + parentId);
		}
		sql = sb.toString();
		// System.out.println(sql);
		return StoreDao.getCount(sql);
	}

	@Override
	public int getPageCount(String permissionName, String parentId, int pageSize) {
		int count = getCount(permissionName, parentId);
		int pageCount = count % pageSize == 0 ? count / pageSize : count / pageSize + 1;
		return pageCount;
	}

}
